<?php

//后台的配置文件
include(dirname(__FILE__).'/config.php');
include(dirname(__FILE__).'/../Inc/tree.class.php');



$dopost = isset($_REQUEST['dopost']) ? trim($_REQUEST['dopost']) : 'list';


/***************************************************************************/
if( 'add' == $dopost ){

	$tid = isset($_GET['tid']) ? intval($_GET['tid']):0;
	//默认值
	$art = array(); 
	//var_dump($_POST);	
	$art = array();
	$art['id'] =  0 ;
	
	$art['title'] =  '' ;
	$art['litpic'] =  '' ;
	$art['url'] =  '' ;

	$art['sort'] =  50;
	$art['display'] =  1 ;//多选框 不选择没有值  
	
	


	$action = '添加';
	include('temp/friendlink_upt.php');
/***************************************************************************/
}else if( 'upt' == $dopost ){
	
	$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
	//读取当前文章
	$sql = "select * from friendlink where id='{$id}'";
	$art = dbRow($sql);
	//var_dump($art);
	
	


	$action = '修改';
	include('temp/friendlink_upt.php');
/***************************************************************************/
}else if( 'save' == $dopost ){
	// addsave  uptsave 


	$id = isset($_POST['id']) ? intval($_POST['id']) : 0;

	//var_dump($_POST);	
	$art = array();
	$art['id'] =  intval($_POST['id']) ;
	
	$art['title'] =  trim($_POST['title']) ;
	$art['litpic'] =  trim($_POST['litpic']) ;
	$art['url'] =  trim($_POST['url']) ;

	$art['sort'] =  intval($_POST['sort']) ;
	$art['display'] =  isset($_POST['display']) ? intval($_POST['display']) : 0  ;//多选框 不选择没有值  
	

	//数据验证... 
	if($art['litpic'] == '' ){ die('{"ret":1,"msg":"图片不能为空！！！"}') ;}
	
	
	

	sec($art);// 递归的 sql 转义字符过滤 
	if($id){
		//修改
		$action = '修改';
		$rs = dbUpdate('friendlink',$art," id = '{$id}'"); // 表 数据 条件

	}else{
		//添加
		$action = '添加';
		$rs = dbInsert('friendlink',$art); // 表 数据 
	}


	if($rs){
		//msgBox($action.'成功!!!' ,'?t='.time() , 2 );
		$j = array();
		$j['ret'] = 0 ;
		$j['msg'] = 'ok!';
		$j['url'] = '?&t='.time() ;

		die( json_encode($j) );

	}else{
		//msgBox($action.'失败!!!' ,'?t='.time() , 10 );
		
		$j = array();
		$j['ret'] = 999 ;
		$j['msg'] = '写入错误!';

		die( json_encode($j) );

	}

/***************************************************************************/
}else if( 'del' == $dopost ){
	$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

	//真删除
	$rs = dbDelete('friendlink'," id = '{$id}'"); // 表 数据 条件
	


	if($rs){
		$j = array();
		$j['ret'] = 0 ;
		$j['msg'] = '删除成功!!!';
		//$j['url'] = '?tid='.$art['tid'].'&t='.time() ;
		die( json_encode($j) );

	}else{
		$j = array();
		$j['ret'] = 999 ;
		$j['msg'] = '删除失败!!!';
		die( json_encode($j) );
	}

/***************************************************************************/
}else if( 'batch_sort' == $dopost ){
	//var_dump($_GET);
	//var_dump($_POST);


	$sort_arr = $_POST['sort'];
	foreach($sort_arr as $k => $v){
		$art = array();
		$art['sort'] = $v;
		$rs = dbUpdate('friendlink',$art," id = '{$k}'"); // 表 数据 条件
	}
	
	die('{"ret":0,"msg":"批量排序成功!!!"}');
	
/***************************************************************************/
}else if( 'batch_display' == $dopost ){



	//var_dump($_GET);
	//var_dump($_POST);
	//$url = $_POST['url'];

	$sort_arr = isset( $_POST['sort'] ) ? $_POST['sort'] : array() ;
	$display_arr = isset( $_POST['display'] ) ? $_POST['display'] : array() ;

	foreach($sort_arr as $k => $v){

		$art = array();
		if( isset( $display_arr[$k] ) && $display_arr[$k] ){
			$art['display'] = 1; 

		}else{
			$art['display'] = 0; 

		}



		$rs = dbUpdate('friendlink',$art," id = '{$k}'"); // 表 数据 条件
	}

	die('{"ret":0,"msg":"更新显示/隐藏 成功 !!!"}');
/***************************************************************************/
}else if( 'list' == $dopost ){

	
    $list = dbList("select * from `friendlink` where 1  order by sort asc , id asc");
    


	include('temp/friendlink_list.php');
/***************************************************************************/
}else{
	die('404');
}
